public class LeetCode35 {
    public static void main(String[] args) {
        //35. 搜索插入位置
        int i = searchInsert(new int[]{1, 2, 3,}, 3);
        System.out.println(i);
    }
    public static int searchInsert(int[] nums, int target) {
        //设置指针和初值
        int i = 0,j = nums.length - 1;
        //i~j 范围内有东西
        while(i<=j){
            int m = (i+j)>>>1;
            //目标值在左边
            if(nums[m]>target){
                j = m-1;
            }
            //目标值在右边
            else if (nums[m] < target){
                i = m+1;
            }
            //找到了返回
            else{
                return m;
            }
        }
        //没找到返回插入的位置
        return i;
    }
}
